package com.wondersgroup.wx;

import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("/wx")
@RestController
public class WxController {

  /**
   *  获取 openId
   */
   @RequestMapping("/getOpenId")
   public Map<String,Object> getOpenId(@RequestBody Map<String,Object> param){
     Map<String,Object> map = new HashMap<>();
     String   code =  param.get("code") != null ? param.get("code").toString() :null;
     if(code == null){
       map.put("code",500);
       map.put("msg","参数不能为空!");
       return map;
    }
    try {
      JSONObject json = OpenIdUtil.getOpenid(code);
      System.out.println("--"+json.toString());
      String openId = json.getString("openid");
      System.out.println("openId.."+openId);
      map.put("code",200);
      map.put("openId",openId);
    } catch (IOException e) {
      e.printStackTrace();
      map.put("code",500);
      map.put("msg","获取openId异常！");
      return map;
    }
     return map;
  }

}
